package com.tanzeer.deliverytracker;

import java.util.ArrayList;

import com.tanzeer.deliverytracker.adapter.SpinnerItemAdapter;
import com.tanzeer.deliverytracker.dbhelper.DBOpenHelper;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CompletedListActivity extends Activity {

	ArrayList<String> spinnerItem = new ArrayList<String>();
	private String trackId;
	private String description;
	private String type;
	private int ID;
	TextView txttracknumebr;
	TextView txtdescription;
	ImageView Imgcategory;
	ActionBar actionbar;
	private static final int BUTTON_POSITIVE = -1;
	private static final int BUTTON_NEGATIVE = -2;
	boolean doubleBackToExitPressedOnce = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delivary_details);
		actionbar = getActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		Intent intent = getIntent();
		trackId = intent.getStringExtra("track_number");
		description = intent.getStringExtra("description");
		type = intent.getStringExtra("type");
		ID = intent.getIntExtra("id", -1);
		txttracknumebr = (TextView) findViewById(R.id.textView2);
		txtdescription = (TextView) findViewById(R.id.textView1);
		Imgcategory = (ImageView) findViewById(R.id.imageView1);
		setTexts();

	}

	@Override
	protected void onStart() {
		overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
		super.onStart();
	}
	private void setTexts() {
		actionbar.setTitle(description);
		txttracknumebr.setText("Tracking Number: "+trackId);
		txtdescription.setText(description);

		String opName = type;
		if (opName.equals("None")) {
			Imgcategory.setBackgroundResource(R.drawable.incoming);
		} else if (opName.equals("Incoming")) {
			Imgcategory.setBackgroundResource(R.drawable.incoming);
		} else if (opName.equals("Outgoing")) {
			Imgcategory.setBackgroundResource(R.drawable.outgoing);
		} else if (opName.equals("Favorite")) {
			Imgcategory.setBackgroundResource(R.drawable.favorite);
		} else if (opName.equals("Movie")) {
			Imgcategory.setBackgroundResource(R.drawable.film_reel);
		} else if (opName.equals("Gift")) {
			Imgcategory.setBackgroundResource(R.drawable.present);
		} else if (opName.equals("Heart")) {
			Imgcategory.setBackgroundResource(R.drawable.heart);
		} else if (opName.equals("Business")) {
			Imgcategory.setBackgroundResource(R.drawable.business);
		} else if (opName.equals("Important")) {
			Imgcategory.setBackgroundResource(R.drawable.important);
		} else if (opName.equals("Camera")) {
			Imgcategory.setBackgroundResource(R.drawable.camera_unmount2);
		} else if (opName.equals("Clothes")) {
			Imgcategory.setBackgroundResource(R.drawable.clothes);
		} else if (opName.equals("Computer")) {
			Imgcategory.setBackgroundResource(R.drawable.mycomputer);
		} else if (opName.equals("Games")) {
			Imgcategory.setBackgroundResource(R.drawable.gaming_pad);
		} else if (opName.equals("Food Dishes")) {
			Imgcategory.setBackgroundResource(R.drawable.fooddishes);
		} else if (opName.equals("Light")) {
			Imgcategory.setBackgroundResource(R.drawable.light);
		} else if (opName.equals("Music")) {
			Imgcategory.setBackgroundResource(R.drawable.kbemusedsrv);
		} else if (opName.equals("Office Materials")) {
			Imgcategory.setBackgroundResource(R.drawable.office);
		} else if (opName.equals("Paint")) {
			Imgcategory.setBackgroundResource(R.drawable.kolourpaint);
		} else if (opName.equals("Medicine")) {
			Imgcategory.setBackgroundResource(R.drawable.pill);
		} else if (opName.equals("Flowers")) {
			Imgcategory.setBackgroundResource(R.drawable.flowers);
		} else if (opName.equals("Sports")) {
			Imgcategory.setBackgroundResource(R.drawable.sports);
		} else if (opName.equals("Tools")) {
			Imgcategory.setBackgroundResource(R.drawable.tool2);
		} else if (opName.equals("None")) {
			Imgcategory.setBackgroundResource(R.drawable.white);
		}
	}

	private void setTexts(String tracks, String des, String types,String str) {
		txttracknumebr.setText(str+": "+tracks);
		txtdescription.setText(des);
		actionbar.setTitle(des);
		// set values
		trackId = tracks;
		description = des;
		type = types;
		String opName = types;
		if (opName.equals("None")) {
			Imgcategory.setBackgroundResource(R.drawable.incoming);
		} else if (opName.equals("Incoming")) {
			Imgcategory.setBackgroundResource(R.drawable.incoming);
		} else if (opName.equals("Outgoing")) {
			Imgcategory.setBackgroundResource(R.drawable.outgoing);
		} else if (opName.equals("Favorite")) {
			Imgcategory.setBackgroundResource(R.drawable.favorite);
		} else if (opName.equals("Movie")) {
			Imgcategory.setBackgroundResource(R.drawable.film_reel);
		} else if (opName.equals("Gift")) {
			Imgcategory.setBackgroundResource(R.drawable.present);
		} else if (opName.equals("Heart")) {
			Imgcategory.setBackgroundResource(R.drawable.heart);
		} else if (opName.equals("Business")) {
			Imgcategory.setBackgroundResource(R.drawable.business);
		} else if (opName.equals("Important")) {
			Imgcategory.setBackgroundResource(R.drawable.important);
		} else if (opName.equals("Camera")) {
			Imgcategory.setBackgroundResource(R.drawable.camera_unmount2);
		} else if (opName.equals("Clothes")) {
			Imgcategory.setBackgroundResource(R.drawable.clothes);
		} else if (opName.equals("Computer")) {
			Imgcategory.setBackgroundResource(R.drawable.mycomputer);
		} else if (opName.equals("Games")) {
			Imgcategory.setBackgroundResource(R.drawable.gaming_pad);
		} else if (opName.equals("Food Dishes")) {
			Imgcategory.setBackgroundResource(R.drawable.fooddishes);
		} else if (opName.equals("Light")) {
			Imgcategory.setBackgroundResource(R.drawable.light);
		} else if (opName.equals("Music")) {
			Imgcategory.setBackgroundResource(R.drawable.kbemusedsrv);
		} else if (opName.equals("Office Materials")) {
			Imgcategory.setBackgroundResource(R.drawable.office);
		} else if (opName.equals("Paint")) {
			Imgcategory.setBackgroundResource(R.drawable.kolourpaint);
		} else if (opName.equals("Medicine")) {
			Imgcategory.setBackgroundResource(R.drawable.pill);
		} else if (opName.equals("Flowers")) {
			Imgcategory.setBackgroundResource(R.drawable.flowers);
		} else if (opName.equals("Sports")) {
			Imgcategory.setBackgroundResource(R.drawable.sports);
		} else if (opName.equals("Tools")) {
			Imgcategory.setBackgroundResource(R.drawable.tool2);
		} else if (opName.equals("None")) {
			Imgcategory.setBackgroundResource(R.drawable.white);
		} else if (opName.equals("")) {
			Imgcategory.setVisibility(View.GONE);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.completed, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.edit) {
			showAddDialog();
		} else if (item.getItemId() == android.R.id.home) {
			moveBack();
			return true;
		} else if (item.getItemId() == R.id.markpending) {
			DBOpenHelper dbHelper = new DBOpenHelper(CompletedListActivity.this);
			dbHelper.updateDelivaryasPending(ID);
			toast("Marked As Pending");
			finish();
			/*
			 * Intent intent=new
			 * Intent(getApplicationContext(),DeliveryListActivity.class);
			 * intent.putExtra("type","Active"); finish();
			 * startActivity(intent);
			 */
			setTexts("", "", "","");

		} else if (item.getItemId() == R.id.remove) {

			showDeleteDialog();

		} else if (item.getItemId() == R.id.shareit) {
			Intent sendIntent = new Intent();
			sendIntent.setAction(Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT, txttracknumebr.getText()
					.toString());
			sendIntent.setType("text/plain");
			startActivity(sendIntent);

		} else if (item.getItemId() == R.id.copy) {
			copyToClipBoard();

		}

		return super.onOptionsItemSelected(item);
	}

	private void copyToClipBoard() {
		ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		ClipData clip = ClipData.newPlainText("simple text", trackId);
		clipboard.setPrimaryClip(clip);

	}

	private void deleteContent() {
		DBOpenHelper dbHelper = new DBOpenHelper(CompletedListActivity.this);
		dbHelper.removeDelivary(ID);
		toast("Delivery Removed");
		finish();
		
	}

	private void showAddDialog() {
		final Dialog dialog = new Dialog(CompletedListActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.addcontent);

		final EditText edtPkgNumber = (EditText) dialog
				.findViewById(R.id.editText1);
		final EditText edtPkgDescription = (EditText) dialog
				.findViewById(R.id.editText2);
		final Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
		edtPkgNumber.setText(trackId);
		edtPkgDescription.setText(description);
		setSpinnerItem();
		// spinner.setSelection(spinnerItem.indexOf(type));
		Log.e("msg", "" + spinnerItem.indexOf(type));
		SpinnerItemAdapter adapter = new SpinnerItemAdapter(
				getApplicationContext(), R.layout.spinner_row_view_custom,
				spinnerItem);
		spinner.setAdapter(adapter);
		spinner.setSelection(spinnerItem.indexOf(type));

		Button btnOk = (Button) dialog.findViewById(R.id.button2);
		Button btnCancel = (Button) dialog.findViewById(R.id.button1);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String tracknumber = edtPkgNumber.getText().toString();
				String descrip = edtPkgDescription.getText().toString();
				String Imgcategory = spinner.getSelectedItem().toString();
				if (!tracknumber.equals("") && !descrip.equals("")
						&& !Imgcategory.equals("")) {
					DBOpenHelper dbhelper = new DBOpenHelper(
							CompletedListActivity.this);
					// dbhelper.saveDelivary(tracknumber, descrip, Imgcategory);
					if (ID > 0) {
						dbhelper.updateDelivary(tracknumber, descrip,
								Imgcategory, ID);
						toast("Data Updated");
					} else
						toast("Updated Failed");
					setTexts(tracknumber, descrip, Imgcategory,"Tracking Number" );
					dialog.dismiss();

				} else
					toast("Some fields are empty");
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void toast(String str) {
		Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
	}

	private void setSpinnerItem() {
		spinnerItem.add("None");
		spinnerItem.add("Incoming");
		spinnerItem.add("Outgoing");
		spinnerItem.add("Favorite");
		spinnerItem.add("Movie");
		spinnerItem.add("Gift");
		spinnerItem.add("Heart");
		spinnerItem.add("Business");
		spinnerItem.add("Important");
		spinnerItem.add("Camera");
		spinnerItem.add("Clothes");
		spinnerItem.add("Computer");
		spinnerItem.add("Games");
		spinnerItem.add("Food Dishes");
		spinnerItem.add("Light");
		spinnerItem.add("Music");
		spinnerItem.add("Office Materials");
		spinnerItem.add("Paint");
		spinnerItem.add("Medicine");
		spinnerItem.add("Flowers");
		spinnerItem.add("Sports");
		spinnerItem.add("Tools");
	}

	private void showDeleteDialog() {

		AlertDialog Alert = new AlertDialog.Builder(CompletedListActivity.this)
				.create();
		Alert.setTitle("Delete");
		Alert.setMessage("Want to remove this  delivery?");

		Alert.setButton(BUTTON_POSITIVE, "Remove",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						deleteContent();
						//setTexts("", "", "","");
						dialog.dismiss();
						// to do
					}
				});

		Alert.setButton(BUTTON_NEGATIVE, "Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();

					}
				});
		Alert.show();
	}
	private void moveBack()
	{
		Intent intentHome = new Intent(getApplicationContext(),
				DeliveryListActivity.class);
		intentHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intentHome.putExtra("type",
				getIntent().getStringExtra("contenttype"));
		intentHome.putExtra("value",1);
		startActivity(intentHome);
		overridePendingTransition(R.anim.anim_in_left,R.anim.anim_out_left);
	}

	@Override
	public void onBackPressed() {

		moveBack();
	}
}
